package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoBusiness;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.SuspiciousTrans;
import com.turing.post.antimoney.util.SuspiciousTransDao;
import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.common.util.IdType;
import com.turing.post.common.util.IdTypeDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.DepositOrder;
import com.turing.post.salesmanage.util.DepositOrderDao;
import com.turing.post.salesmanage.util.DepositOrderPaidDetail;
import com.turing.post.salesmanage.util.DepositOrderPaidDetailDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.Profession;
import com.turing.post.salesmanage.util.ProfessionDao;
import com.turing.post.salesmanage.util.RedempOrder;
import com.turing.post.salesmanage.util.RedempOrderDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.salesmanage.util.SaleOrderPaidDetail;
import com.turing.post.salesmanage.util.SaleOrderPaidDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author 李治
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmInfoBusinessImpl")
public class AlarmInfoBusinessImpl implements AlarmInfoBusiness {

	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "antimoney.impl.SuspiciousTransDaoImpl")
	private SuspiciousTransDao suspiciousTransDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;
	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;
	@EJB(mappedName = "salesmanage.impl.SaleOrderPaidDetailDaoImpl")
	private SaleOrderPaidDetailDao saleOrderPaidDetailDaoImpl;
	@EJB(mappedName = "capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDaoImpl;
	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "common.impl.IdTypeDaoImpl")
	private IdTypeDao idTypeDaoImpl;
	@EJB(mappedName = "salesmanage.impl.ProfessionDaoImpl")
	private ProfessionDao professionDaoImpl;
	@EJB(mappedName = "salesmanage.impl.DepositOrderDaoImpl")
	private DepositOrderDao depositOrderDaoImpl;
	@EJB(mappedName = "salesmanage.impl.DepositOrderPaidDetailDaoImpl")
	private DepositOrderPaidDetailDao depositOrderPaidDetailDaoImpl;
	@EJB(mappedName = "salesmanage.impl.RedempOrderDaoImpl")
	private RedempOrderDao redempOrderDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@Override
	public void dealAlarmInfo(PCPSWebInput webInput, AlarmInfo alarmInfo)
			throws PCPSApplicationException {
		if (null == alarmInfo || null == alarmInfo.getAlarmInfoId()) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空");
		}
		if (null == webInput || null == webInput.getOperId()) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"业务处理webservice输入值为空");
		}
		AlarmInfo _AlarmInfo = alarmInfoDaoImpl.getAlarmInfoById(alarmInfo
				.getAlarmInfoId());
		if (null == _AlarmInfo) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"预警信息不存在");
		}
		if ("1".equals(_AlarmInfo)) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"当前预警信息已处理");
		}
		_AlarmInfo.setDealStatus("1");
		_AlarmInfo.setDealOperId(webInput.getOperId());
		_AlarmInfo.setDealTime(commonDaoImpl.getDatabaseSysTime());
		_AlarmInfo.setDealResult(alarmInfo.getDealResult());
		_AlarmInfo.setNormalOrSuspicious(alarmInfo.getNormalOrSuspicious());
		_AlarmInfo.setRemark(alarmInfo.getRemark());
		alarmInfoDaoImpl.updateAlarmInfo(_AlarmInfo);
		// 可疑交易处理
		if ("1".equals(alarmInfo.getNormalOrSuspicious())) {
			dealSuspiciousTrans(_AlarmInfo);
		}

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "预警信息处理", "预警信息ID:"
						+ _AlarmInfo.getAlarmInfoId() + "预警原因："
						+ _AlarmInfo.getAlarmReason());
	}

	/**
	 * 处理可疑交易
	 * 
	 * @param alarmInfo
	 * @throws PCPSApplicationException
	 */
	private void dealSuspiciousTrans(AlarmInfo alarmInfo)
			throws PCPSApplicationException {
		List<AlarmInfoDetail> alarmInfoDetails = alarmInfoDetailDaoImpl
				.getAllAlarmInfoDetailsByAlarmInfoId(alarmInfo.getAlarmInfoId());
		// 添加可疑交易明细
		SuspiciousTrans s =null;
		for (AlarmInfoDetail ad : alarmInfoDetails) {
			s=new SuspiciousTrans();
			s.setAlarmInfoId(ad.getAlarmInfoId());// 预警信息Id
			s.setReportId(null);
			s.setSeqNo(ad.getSeqNo());// 顺序号
			s.setTSTM(ad.getTranDate());// 交易时间
			s.setTICD(ad.getTranSeqNo());// 业务标识号
			s.setSMID(ad.getMerchantNo());// 可疑主体特约商户编号
			s.setCRTP("CNY");// 交易币种
			if ("0".equals(ad.getBusinessType())) {// 业务类型为销售单
				dealSaleOrder(ad, s);
			}
			if ("1".equals(ad.getBusinessType())) {// 业务类型为充值单
				dealDepositOrder(ad, s);
			}
			if ("2".equals(ad.getBusinessType())) {// 业务类型为赎回单
				dealRedempOrder(ad, s);
			}
			if ("3".equals(ad.getBusinessType())) {// 业务类型为交易流水
				dealTradDetail(ad, s);
			}
			suspiciousTransDaoImpl.insertSuspiciousTrans(s);
		}
	}

	/**
	 * 处理业务类型为交易流水
	 * 
	 * @param ad
	 * @param s
	 */
	private void dealTradDetail(AlarmInfoDetail ad, SuspiciousTrans s) {
		//
	}

	/**
	 * 加载业务类型为赎回单的数据
	 * 
	 * @param ad
	 * @param s
	 */
	private void dealRedempOrder(AlarmInfoDetail ad, SuspiciousTrans s)throws PCPSApplicationException {
		if(ad.getBusinessOrderId()==null){
			throw new PCPSApplicationException(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
				"业务单id为空!");
		}
		// 获取赎回单实体
		RedempOrder redempOrder = redempOrderDaoImpl
				.getRedempOrderByRedempOrderId(ad.getBusinessOrderId());
		if (null != redempOrder) {
			BigInteger redempId = null;// 赎回人ID
			if (null != redempOrder.getPersonalBuyerId()) {// 个人赎回
				redempId = redempOrder.getPersonalBuyerId();
			} else if (null != redempOrder.getOrganizeBuyerId()
					&& null != redempOrder.getProxyBuyerId()) {// 单位赎回
				redempId = redempOrder.getProxyBuyerId();
			}
			PersonalCustom pc = personalCustomDaoImpl
					.getPersonalCustomByPersonalId(redempId);
			if (null != pc) {
				s.setCTNM(pc.getName());// 可疑主体名称
				IdType idType = idTypeDaoImpl.getIdTypeByIdTypeId(pc
						.getIdTypeId());
				if (null != idType) {
					s.setCITP(idType.getIdTypeNo());// 可疑主体身份证件/证明文件类型
				}
				s.setCTID(pc.getIdNo());// 可疑主体身份证件/证明文件号码
				s.setCTAR(pc.getLinkAddr()); // 详细住址
				s.setCCTL(pc.getLinkTel());// 联系电话
				s.setCEML(pc.getEmail());// 电子邮件
				Profession profession = professionDaoImpl
						.getProfessionByProfessionId(pc.getProfessionId());
				if (null != profession
						&& !PCPSUtil.isEmpty(profession.getProfessionNo())) {
					String ctvc = profession.getProfessionNo();
					Profession supProfession = profession;
					while (ctvc.length() > 2) {
						Profession sPro = professionDaoImpl
								.getProfessionByProfessionNo(supProfession
										.getSuperProfessionNo());
						supProfession = sPro;
						ctvc = sPro.getProfessionNo();
					}
					if (1 == ctvc.length())
						ctvc = "1" + ctvc;
					s.setCTVC(ctvc);// 可疑主体的职业/行业类别
				}
			}
		}
		s.setCRAT(redempOrder.getTotalRedempAmount());// 交易金额
		// 交易对手信息处理
		BankAccount bankAccount = bankAccountDaoImpl
				.getBankAccountByBankAccountId(redempOrder.getPayeeAccountId());
		if (null != bankAccount) {
			s.setTCBA(bankAccount.getAccountNo());// 交易对手的银行账号
			s.setTCBN(bankAccount.getAccountName());// 交易对手银行账号的开户银行名称
			s.setTCTA(bankAccount.getAccountNo());// 交易对手的支付账户号码
		}
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(redempOrder.getSaleBranchNo());
		if (null != saleBranch) {
			s.setTCNM(saleBranch.getSaleBranchName());// 交易对手姓名/名称
			// 得不到 TSMI 交易对手特约商户编号
			// 得不到 TCIT 交易对手证件/证明文件类型
			// 得不到 TCID 交易对手证件/证明文件号码

			// TCAT 交易对手的银行账号种类
			// TCTT 交易对手的支付账户种类
			// TCTA 交易对手的支付账户号码
			// TCPN 交易对手所在支付机构的名称
			// TCPA 交易对手所在支付机构的银行账号
			// TPBN 交易对手所在支付机构银行账号的开户银行名称
			// TCIP 交易对手的交易IP地址
			// TMNM 交易商品名称
			// BPTC 银行与支付机构之间的业务交易编码
			// PMTC 支付机构与商户之间的业务交易编码
		}
	}

	/**
	 * 加载业务类型为充值单的数据
	 * 
	 * @param ad
	 * @param s
	 */
	private void dealDepositOrder(AlarmInfoDetail ad, SuspiciousTrans s)throws PCPSApplicationException {
		if(ad.getBusinessOrderId()==null){
			throw new PCPSApplicationException(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
				"业务单id为空!");
		}
		// 获取充值单实体
		DepositOrder depositOrder = depositOrderDaoImpl
				.getDepositOrderByDepositOrderId(ad.getBusinessOrderId());
		if (null != depositOrder) {
			// 可疑主体基本信息
			BigInteger depositId = null;// 充值人ID
			if ("0".equals(depositOrder.getBuyerType())
					&& null != depositOrder.getPersonalBuyerId()) {// 个人充值
				depositId = depositOrder.getPersonalBuyerId();
			} else if ("1".equals(depositOrder.getBuyerType())// 单位充值
					&& null != depositOrder.getOrganizeBuyerId()) {
				depositId = depositOrder.getProxyBuyerId();
			}
			if (null != depositId) {
				PersonalCustom pc = personalCustomDaoImpl
						.getPersonalCustomByPersonalId(depositId);
				if (null != pc) {
					s.setCTNM(pc.getName());// 可疑主体名称
					IdType idType = idTypeDaoImpl.getIdTypeByIdTypeId(pc
							.getIdTypeId());
					if (null != idType) {
						s.setCITP(idType.getIdTypeNo());// 可疑主体身份证件/证明文件类型
					}
					s.setCTID(pc.getIdNo());// 可疑主体身份证件/证明文件号码
					s.setCTAR(pc.getLinkAddr()); // 详细住址
					s.setCCTL(pc.getLinkTel());// 联系电话
					s.setCEML(pc.getEmail());// 电子邮件
					Profession profession = professionDaoImpl
							.getProfessionByProfessionId(pc.getProfessionId());
					if (null != profession
							&& !PCPSUtil.isEmpty(profession.getProfessionNo())) {
						String ctvc = profession.getProfessionNo();
						Profession supProfession = profession;
						while (ctvc.length() > 2) {
							Profession sPro = professionDaoImpl
									.getProfessionByProfessionNo(supProfession
											.getSuperProfessionNo());
							supProfession = sPro;
							ctvc = sPro.getProfessionNo();
						}
						if (1 == ctvc.length())
							ctvc = "1" + ctvc;
						s.setCTVC(ctvc);// 可疑主体的职业/行业类别
					}
				}
			}
			// 可疑主体基本信息完成
			// 可疑主体银行账户信息
			List<DepositOrderPaidDetail> depositOrderPaidDetails = depositOrderPaidDetailDaoImpl
					.getDepositOrderPaidDetailListByDepositOrderId(depositOrder
							.getDepositOrderId());
			for (DepositOrderPaidDetail dopd : depositOrderPaidDetails) {
				if ("2".equals(dopd.getPaidType())) {// 银行转账
					BankAccount bankAccount = bankAccountDaoImpl
							.getBankAccountByBankAccountId(dopd
									.getPaidAccountId());
					if (null != bankAccount) {
						String bankType = "0".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_ISSUECOMPANY
								: "1".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_BRANCH
										: "2".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_CUSTOM
												: "";
						s.setCBAT(bankType);// 可疑主体的银行账号种类
						s.setCBAC(bankAccount.getAccountNo()); // 可疑主体的银行账号
						s.setCABM(bankAccount.getBankName());// 可疑主体银行账号的开户银行名称
						break;
						// s.setCTAT();// 可疑主体的支付账户种类
						// s.setCTAC();// 可疑主体的支付账户号码
						// s.setCPIN();// 可疑主体所在支付机构的名称
						// s.setCPBA();// 可疑主体所在支付机构的银行账号
						// s.setCPBN();// 可疑主体所在支付机构银行账号的开户银行名称
					}
				}
			}
			// 可以主体银行账户信息完成
		}
		s.setCRAT(depositOrder.getTotalDepositAmount());// 交易金额
		// 处理交易对手数据
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(depositOrder.getSaleBranchNo());
		if (null != saleBranch) {
			s.setTCNM(saleBranch.getSaleBranchName());// 交易对手姓名/名称
			// 得不到 TSMI 交易对手特约商户编号
			// 得不到 TCIT 交易对手证件/证明文件类型
			// 得不到 TCID 交易对手证件/证明文件号码

			// TCAT 交易对手的银行账号种类
			// TCBA 交易对手的银行账号
			// TCBN 交易对手银行账号的开户银行名称
			// TCTT 交易对手的支付账户种类
			// TCTA 交易对手的支付账户号码
			// TCPN 交易对手所在支付机构的名称
			// TCPA 交易对手所在支付机构的银行账号
			// TPBN 交易对手所在支付机构银行账号的开户银行名称
			// TCIP 交易对手的交易IP地址
			// TMNM 交易商品名称
			// BPTC 银行与支付机构之间的业务交易编码
			// PMTC 支付机构与商户之间的业务交易编码

		}
	}

	/**
	 * 加载业务类型为销售单的数据
	 * 
	 * @param ad
	 * @param s
	 */
	private void dealSaleOrder(AlarmInfoDetail ad, SuspiciousTrans s)throws PCPSApplicationException {
		if(ad.getBusinessOrderId()==null){
			throw new PCPSApplicationException(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
				"业务单id为空!");
		}
		// 获取销售单实体
		SaleOrder saleOrder = saleOrderDaoImpl.getSaleOrderBySaleOrderId(ad
				.getBusinessOrderId());
		if (null != saleOrder) {
			// 可疑主体基本信息
			BigInteger cardBuyerId = null;// 购卡人ID
			if ("0".equals(saleOrder.getBuyerType())
					&& null != saleOrder.getPersonalBuyerId()) {// 为个人购卡
				cardBuyerId = saleOrder.getPersonalBuyerId();
			} else if ("1".equals(saleOrder.getBuyerType())// 为单位购卡
					&& null != saleOrder.getOrganizeBuyerId()) {
				cardBuyerId = saleOrder.getProxyBuyerId();
			}
			if (null != cardBuyerId) {
				PersonalCustom pc = personalCustomDaoImpl
						.getPersonalCustomByPersonalId(cardBuyerId);
				if (null != pc) {
					s.setCTNM(pc.getName());// 可疑主体名称
					IdType idType = idTypeDaoImpl.getIdTypeByIdTypeId(pc
							.getIdTypeId());
					if (null != idType) {
						s.setCITP(idType.getIdTypeNo());// 可疑主体身份证件/证明文件类型
					}
					s.setCTID(pc.getIdNo());// 可疑主体身份证件/证明文件号码
					s.setCTAR(pc.getLinkAddr()); // 详细住址
					s.setCCTL(pc.getLinkTel());// 联系电话
					s.setCEML(pc.getEmail());// 电子邮件
					Profession profession = professionDaoImpl
							.getProfessionByProfessionId(pc.getProfessionId());
					if (null != profession
							&& !PCPSUtil.isEmpty(profession.getProfessionNo())) {
						String ctvc = profession.getProfessionNo();
						Profession supProfession = profession;
						while (ctvc.length() > 2) {
							Profession sPro = professionDaoImpl
									.getProfessionByProfessionNo(supProfession
											.getSuperProfessionNo());
							supProfession = sPro;
							ctvc = sPro.getProfessionNo();
						}
						if (1 == ctvc.length())
							ctvc = "1" + ctvc;
						s.setCTVC(ctvc);// 可疑主体的职业/行业类别
					}
				}
			}
			// 可疑主体基本信息完成
			// 可疑主体银行账户信息
			List<SaleOrderPaidDetail> saleOrderPaidDetails = saleOrderPaidDetailDaoImpl
					.getSaleOrderPaidDetailListBySaleOrderId(saleOrder
							.getSaleOrderId());
			for (SaleOrderPaidDetail sopd : saleOrderPaidDetails) {
				if ("2".equals(sopd.getPaidType())) {// 银行转账
					BankAccount bankAccount = bankAccountDaoImpl
							.getBankAccountByBankAccountId(sopd
									.getPaidAccountId());
					if (null != bankAccount) {
						String bankType = "0".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_ISSUECOMPANY
								: "1".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_BRANCH
										: "2".equals(bankAccount.getBankType()) ? BankAccount.BANKTYPE_CUSTOM
												: "";
						s.setCBAT(bankType);// 可疑主体的银行账号种类
						s.setCBAC(bankAccount.getAccountNo()); // 可疑主体的银行账号
						s.setCABM(bankAccount.getBankName());// 可疑主体银行账号的开户银行名称
						break;
						// s.setCTAT();// 可疑主体的支付账户种类
						// s.setCTAC();// 可疑主体的支付账户号码
						// s.setCPIN();// 可疑主体所在支付机构的名称
						// s.setCPBA();// 可疑主体所在支付机构的银行账号
						// s.setCPBN();// 可疑主体所在支付机构银行账号的开户银行名称
					}
				}
			}
			// 可以主体银行账户信息完成
		}
		s.setCRAT(saleOrder.getTotalSaleAmount());// 交易金额
		// 处理交易对手数据
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(saleOrder.getSaleBranchNo());
		if (null != saleBranch) {
			s.setTCNM(saleBranch.getSaleBranchName());// 交易对手姓名/名称
			// 得不到 TSMI 交易对手特约商户编号
			// 得不到 TCIT 交易对手证件/证明文件类型
			// 得不到 TCID 交易对手证件/证明文件号码

			// TCAT 交易对手的银行账号种类
			// TCBA 交易对手的银行账号
			// TCBN 交易对手银行账号的开户银行名称
			// TCTT 交易对手的支付账户种类
			// TCTA 交易对手的支付账户号码
			// TCPN 交易对手所在支付机构的名称
			// TCPA 交易对手所在支付机构的银行账号
			// TPBN 交易对手所在支付机构银行账号的开户银行名称
			// TCIP 交易对手的交易IP地址
			// TMNM 交易商品名称
			// BPTC 银行与支付机构之间的业务交易编码
			// PMTC 支付机构与商户之间的业务交易编码
		}
	}
}
